<?php
session_start();
define('TEMPLATES', '../templates/');
define('JS','../js/');
define('CSS','../css/');
define('OBJETOS','../objetos/');
define('NEGOCIOS','../negocio/');
define('EXTENSION','.phtml');
function __autoload($class_name){
  require_once $class_name .'.php';
}
 
Class TransaccionReserva {
	
	var $AdminObj;
	var $generador_xhtml;
	var $vars;
	
	function TransaccionReserva($array_vars){
		$this->AdminObj=new AdminReserva();
		$this->vars=$array_vars;
		$this->generador_xhtml=new GeneradorXHTML();
	}

	function transNewReserva(){
		$reserva=$this->AdminObj->getNewObject("Reserva",array("codigo_reserva"=>$this->AdminObj->CodigoAlfanumerico(8)));
		$options_cate_h=$this->generador_xhtml->getOptions($this->AdminObj->getCategorias());
		$options_regimen=$this->generador_xhtml->getOptions($this->AdminObj->getRegimenes());
		$this->generador_xhtml->mostrarTpl($reserva,array("tpl"=>"Reserva","css"=>"ui.datepicker.css.php,reserva.css","js"=>"Reserva.js,Validator.js","accion"=>"transSetReserva","boton"=>"GUARDAR","options_categorias_h"=>$options_cate_h,"regimenes"=>$options_regimen));
	}
	
	function transSetReserva(){
	    $this->vars['id_persona']=$this->AdminObj->getNewID("Persona","id_persona");
		$this->vars['id_cliente']=$this->AdminObj->getNewID("Cliente","id_cliente");
		$this->vars['estado_reserva']='RECEPCIONADO';
		$this->vars['fecha_reserva']=date("Y/m/d");
		$this->vars['fecha_deposito']=date("Y/m/d");
		$this->vars['fnac_persona']="1/1/1";
		$this->vars['id_persona_cliente']=$this->vars['id_persona'];
		$this->vars['id_cliente_reserva']=$this->vars['id_cliente'];
		$this->vars['id_empresa_cliente']='NULL';		
		$this->vars['id_hotel_reserva']=$_SESSION['S_HOTEL'];
		$this->vars['concepto_deposito']='Pago de recepcion en hotel';
		$persona=$this->AdminObj->getNewObject("Persona",$this->vars);
		$cliente=$this->AdminObj->getNewObject("Cliente",$this->vars);
		
		$this->AdminObj->setObjectDB($persona,"persona");
    	$this->AdminObj->setObjectDB($cliente,"cliente");
	    $cod_habitaciones=split(';',$this->vars['id_habitacion_reserva']);
        $fini_reservas=split(';',$this->vars['fini_reserva']);
        $ffin_reservas=split(';',$this->vars['ffin_reserva']);
        foreach($cod_habitaciones as $key=>$id){
		    $this->vars['fini_reserva']=next(split(" ",$fini_reservas[$key]));  
			$this->vars['ffin_reserva']=next(split(" ",$ffin_reservas[$key])); 
            $fini=split("/",$this->vars['fini_reserva']);
            $ffin=split("/",$this->vars['ffin_reserva']);
            $this->vars['fini_reserva']=$fini[2]."-".$fini[1]."-".$fini[0];  
			$this->vars['ffin_reserva']=$ffin[2]."-".$ffin[1]."-".$ffin[0]; 
            $this->vars['id_habitacion_reserva']=$id;
			$this->vars['id_reserva']=$this->AdminObj->getNewID("Reserva","id_reserva");
            $this->vars['codigo_reserva']=$this->vars['id_reserva'].$this->vars['codigo_reserva'];
			$this->vars['id_deposito']=$this->AdminObj->getNewID("Deposito","id_deposito");
			$this->vars['id_reserva_deposito']=$this->vars['id_reserva'];
            $reserva=$this->AdminObj->getNewObject("Reserva",$this->vars);
		    $deposito=$this->AdminObj->getNewObject("Deposito",$this->vars);
		    $this->AdminObj->setObjectDB($reserva,"reserva");
			$this->AdminObj->setObjectDB($deposito,"deposito");
		}
	$this->AdminObj->redireccionar("TransaccionReserva.php");
	}
	
	function transGetReserva(){
		$Persona=$this->AdminObj->getPersona($this->vars['id_persona']);
		$array_elementos=array("tpl"=>"Persona"  ,"boton"=>"Guardar cambios","accion"=>"transUpdatePersona","css"=>"Persona","js"=>"Persona");
		$this->generador_xhtml->mostrarTpl($Persona,$array_elementos);
	}
	
	function transUpdateReserva(){
		$Persona=$this->AdminObj->getNewObject("Persona",$this->vars);
		$this->AdminObj->updatePersona($Persona);
		$this->AdminObj->redireccionar("TransaccionPersona.php");	
	}
	
	function transDeleteReserva(){
		$this->AdminObj->deletePersona($this->AdminObj->getPersona($this->vars['id_persona']));
		//$this->AdminObj->redireccionar("TransaccionPersona.php");
	}
	function transGetReservas(){
		$rows=$this->generador_xhtml->generarRowsTable($this->AdminObj->getReservas(),"checkbox",false,0);
		$this->generador_xhtml->mostrarTpl("",array("tpl"=>"Reservas","rows"=>$rows,"js"=>"Reservas.js"));
	}
	
	function transGetHabitacionesDis(){
	$fini=split("/",$this->vars['fech_checkin']);
    $ffin=split("/",$this->vars['fech_checkout']);   
	$habitaciones=$this->generador_xhtml->generarRowsTable4($this->AdminObj->getHabitacionesDis($this->vars['id_categoria'],$fini[2]."-".$fini[1]."-".$fini[0],$ffin[2]."-".$ffin[1]."-".$ffin[0],$this->vars['id_regimen']),'checkbox');
    $this->AdminObj->setHeaderLatin();
    echo $habitaciones;
	}
			
}

if($_GET){
	$vars_trans=array_slice($_GET,1);
//antes no olvidar poner una validador para que nadie pueda ejecutar las funciones
	call_user_func(array(new TransaccionReserva($vars_trans),current($_GET)?current($_GET):"transGetReservas"));
}
else if($_POST){
		$vars_trans=array_slice($_POST,1);
		call_user_func(array(new TransaccionReserva($vars_trans),current($_POST)?current($_POST):"transGetReservas"));
		}
	else	call_user_func(array(new TransaccionReserva(array()),"transGetReservas"));

?>